// 用户登录表单验证
let validation=[];

validation[0]={
    username: [
        {required: true, message: '请输入用户名'},
        {minlength: 11, message: '用户名长度为11个字符'},
        {maxlength: 11, message: '用户名长度为11个字符'},
        {checked: ['^1[3|4|5|8|7][0-9]\\d{8}$'], message: '用户名格式错误'}
    ]
};

validation[1]={
    password: [
        {required: true, message: `请输入密码`}
    ]
};

validation[2]={
    phone: [
        {required: true, message: '请输入手机号'},
        {minlength: 11, message: '手机号长度为11个字符'},
        {maxlength: 11, message: '手机号长度为11个字符'},
        {checked: '^1[3|4|5|8|7][0-9]\\d{8}$', message: '手机号格式错误'}
    ]
};
validation[3]={
    idcard: [
        {required: true, message: '请输入身份证号'},
        {checked: ['^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$'], message: '身份证格式错误'}
    ]
};

validation[4]={
    email: [
        {required: true, message: '邮箱不能为空'},
        {checked: ['^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$'], message: '邮箱格式错误'}
    ]
};

//把数组对象合并后返回
let getValidation=(...args)=>{

    let getOnleKeyArray=  Array.from(new Set([...args]));// args 去重后 转化为 Array
    let objCont={}
    getOnleKeyArray.map((value,key)=>{
        Object.assign(objCont,validation[value])
    })
    return objCont
}
//默认暴露外部的变量
export  default {
    loginValidation:getValidation(2,1),


}




export const checkForm = function (obj, rules) {

    let errormessage=''
    for (let i in obj) {
        for (let j in rules) {
            if (i === j) {
                for (let m in rules[j]) {
                    let val = rules[j][m]
                    for (let check in val) {
                        if (check === 'required') {
                            if (obj[i] === '') {
                                errormessage = val.message
                                return errormessage
                            }
                        } else if (check === 'minlength') {
                            if (obj[i].length < val[check]) {
                                errormessage = val.message
                                return errormessage
                            }
                        } else if (check === 'maxlength') {
                            if (obj[i].length > val[check]) {
                                errormessage = val.message
                                return errormessage
                            }
                        } else if (check === 'checked') {
                            if (typeof (val[check]) !== 'string') {
                                for (let v of val[check]) {
                                    if ((!new RegExp(v).test(obj[i]))) {
                                        errormessage = val.message
                                        return errormessage
                                    }
                                }
                            } else {
                                if ((!new RegExp(val[check]).test(obj[i]))) {
                                    errormessage = val.message
                                    return errormessage
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}